home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 12 / Cream of the Crop 12 (Part II) / Cream of the Crop 12 (Part II).iso / OS2 / DES3_OS2.ZIP / TESTECB.C < prev   
Encoding:
C/C++ Source or Header  |  1996-02-20  |  1.8 KB  |  45 lines

  1. /*
  2.  * TESTECB.C - validates Data Encryption Standard (DES) Algorithm in ECB mode 
  3.  *
  4.  *  The Electronic Codebook (ECB) mode is a basic, block, cryptographic
  5.  * method which transfroms 64 bits of input to 64 bit of output. The same
  6.  * plain text block always produces the same cipher text block for a given
  7.  * cryptographic key.
  8.  */
  9. #include <stdio.h>
  10. #include "des3.h"    /* DES function prototypes */
  11.  
  12.  
  13. /* size of standard DES block, in bits */
  14. #define    BITSZ    (64)
  15. /* size of standard DES block, in bytes */
  16. #define    BLKSZ    (8)
  17. /* how many standard DES blocks defined in ANSI X9.9 Appendix B */
  18. #define    X99MX    (4)
  19. void main(void)
  20. {
  21.   /* for validation, keybits and patterns taken from ANSI X9.9 Appendix B */
  22.   static unsigned char keybits[BLKSZ] = { 0xe6,0xa1,0x2f,0x07,0x9d,0x15,0xc4,0x37 };
  23.   static unsigned char pattern[X99MX][BLKSZ] = {
  24.        { 0x0a,0x20,0x20,0x20,0x54,0x4f,0x20,0x59 },
  25.        { 0x51,0x44,0x2d,0x38,0x30,0x20,0x30,0x37 },
  26.        { 0x54,0x4f,0x20,0x59,0x4f,0x55,0x52,0x20 },
  27.        { 0x51,0x44,0x2d,0x38,0x30,0x20,0x30,0x37 }
  28.   };
  29.   unsigned char results[X99MX*BLKSZ], clrtext[X99MX*BLKSZ];
  30.   int i;
  31.   desinit(keybits);
  32.   for (i = 0; i < X99MX; i++) {
  33.     ecbencode(pattern[i],results);
  34.     ecbdecode(results,clrtext);
  35.     printf("ECB %02x%02x%02x%02x%02x%02x%02x%02x -> %02x%02x%02x%02x%02x%02x%02x%02x -> %02x%02x%02x%02x%02x%02x%02x%02x\n",
  36.             pattern[i][0], pattern[i][1], pattern[i][2], pattern[i][3],
  37.             pattern[i][4], pattern[i][5], pattern[i][6], pattern[i][7],
  38.             results[0], results[1], results[2], results[3],
  39.             results[4], results[5], results[6], results[7],
  40.             clrtext[0], clrtext[1], clrtext[2], clrtext[3],
  41.             clrtext[4], clrtext[5], clrtext[6], clrtext[7] );
  42.    }
  43.    printf("\n");
  44. }
  45.